package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpRequest;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.entity.AlarmPerimeterDev;
import com.ruoyi.system.mapper.AlarmPerimeterDevMapper;
import com.ruoyi.system.service.AlarmPerimeterDevService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (AlarmPerimeterDev)表服务实现类
 *
 * @since 2023-04-21 15:35:21
 */
@Slf4j
@Service("alarmPerimeterDevService")
public class AlarmPerimeterDevServiceImpl implements AlarmPerimeterDevService {
    @Resource
    private AlarmPerimeterDevMapper alarmPerimeterDevDao;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private RedisCache redisCache;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public AlarmPerimeterDev queryById(String devsn) {
        return this.alarmPerimeterDevDao.queryById(devsn);
    }

    @Override
    @SuppressWarnings("all")
    public Integer countAlarmPerimeterDev(AlarmPerimeterDev alarmPerimeterDev) {
        String sql = "SELECT count(1) FROM alarm_perimeter_dev";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    @SuppressWarnings("all")
    public List<AlarmPerimeterDev> queryByPage(AlarmPerimeterDev alarmPerimeterDev, int pageNum, int pageSize) {
        String sql = "SELECT * FROM alarm_perimeter_dev limit " + pageNum + "," + pageSize;
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(AlarmPerimeterDev.class));
    }

    /**
     * 新增数据
     *
     * @param alarmPerimeterDev 实例对象
     * @return 实例对象
     */
    @Override
    public AlarmPerimeterDev insert(AlarmPerimeterDev alarmPerimeterDev) {
        this.alarmPerimeterDevDao.insert(alarmPerimeterDev);
        return alarmPerimeterDev;
    }

    /**
     * 修改数据
     *
     * @param alarmPerimeterDev 实例对象
     * @return 实例对象
     */
    @Override
    public AlarmPerimeterDev update(AlarmPerimeterDev alarmPerimeterDev) {
        this.alarmPerimeterDevDao.update(alarmPerimeterDev);
        return this.queryById(alarmPerimeterDev.getDevsn());
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String devsn) {
        return this.alarmPerimeterDevDao.deleteById(devsn) > 0;
    }


    /**
     * 获取一号防区设备状态
     */
    @Scheduled(cron = "0/60 * * * * ?")
    public void getOneSectorDeviceStatus() {
        String url = "http://192.168.18.139:25110/DevCtrl/GetSectorStatus";

        String body = HttpRequest.post(url).contentType("application/x-www-form-urlencoded").body("param={\"DeviceId\":00000001}").execute().body();

        redisCache.setCacheObject("获取一号防区设备状态", body);

        log.info("获取一号防区设备状态：{}", body);

    }


}
